package com.rybbaby.tss.front.tssClassSchedule.service.imp;

import com.rybbaby.tss.core.utils.DateUtil;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssClassSchedule.entity.ViewTssClassSchedule;
import com.rybbaby.tss.front.tssClassSchedule.service.IViewTssClassScheduleService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 雷春琼
 * @description 课表表数据库操作服务实现
 * @createDate 2017-08-09 下午11:52:12
 */
@Service
@Transactional
public class ViewTssClassScheduleServiceImp extends BaseServiceImp<ViewTssClassSchedule> implements IViewTssClassScheduleService {


    @Override
    public List<Map<String, Object>> viewTssClassSchedule(Integer type) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Timestamp(System.currentTimeMillis());
        String monday = sdf.format(DateUtil.getThisWeekMonday(date));
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, 6);// num为增加的天数，可以改变的
        String sunday = sdf.format(ca.getTime());

        String weekDaySql = "select weekDay from view_tss_class_schedule where type=?  AND classTime BETWEEN ? AND ? GROUP BY weekDay ";
        List<Map<String, Object>> weekList = this.findList(weekDaySql, new Object[]{type, monday, sunday});
        List<Map<String, Object>> classScheduleMapList = new ArrayList<>();
        try {
            for (Map<String, Object> weeks : weekList) {
                Map<String, Object> weeksMap = new HashedMap();
                weeksMap.put("weekDay", weeks.get("weekDay"));
                classScheduleMapList.add(weeksMap);
            }
            List<Map<String, Object>> classScheduleList = null;
            for (Map<String, Object> map : classScheduleMapList) {
                classScheduleList = new ArrayList<>();
                Integer weeks = Integer.valueOf(map.get("weekDay") + "");
                String sql = "select roomName,roomId,weekDay from view_tss_class_schedule where type=? and weekDay=? AND classTime BETWEEN ? AND ? group by roomId order by weekDay asc";
                List<Map<String, Object>> mapList = this.findList(sql, new Object[]{type, weeks, monday, sunday});
                for (Map<String, Object> classMap : mapList) {
                    String classSql = "select * from view_tss_class_schedule where type=? and weekDay=? and roomId=? AND classTime BETWEEN ? AND ? order by startTime asc";
                    List<Map<String, Object>> classMapList = this.findList(classSql, new Object[]{type, weeks, classMap.get("roomId") + "", monday, sunday});
                    classMap.put("classInfoList", classMapList);
                }
                classScheduleList.addAll(mapList);
                map.put("roomInfoList", classScheduleList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return classScheduleMapList;
    }
}
